package com.icode.core.model;

import com.icode.core.shared.usertype.AuthorityListUserType;
import com.icode.core.shared.usertype.SingleColumnMapUserType;
import com.icode.core.shared.usertype.StringSetUserType;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.*;

/**
 * Created by Administrator on 2015/1/19.
 * <p/>
 * it indicates the third party application
 */
@Entity
@Table
@TypeDefs(value = {
        @TypeDef(name = "stringSet", typeClass = StringSetUserType.class, defaultForType = Set.class),
        @TypeDef(name = "authorityList", typeClass = AuthorityListUserType.class, defaultForType = List.class),
        @TypeDef(name = "map", typeClass = SingleColumnMapUserType.class, defaultForType = Map.class)
})
public class Client extends AbstractEntity implements ClientDetails {
    @Column(name = "client_id", unique = true)
    private String clientId;

    @Column(name = "client_secret")
    private String clientSecret;

    @Column(name = "scopes")
    private Set<String> scope = Collections.emptySet();

    @Column(name = "resource_ids")
    private Set<String> resourceIds = Collections.emptySet();

    @Column(name = "grant_types")
    private Set<String> authorizedGrantTypes = Collections.emptySet();

    @Column(name = "redirect_uri")
    private Set<String> registeredRedirectUris = Collections.emptySet();

    @Column(name = "auto_approve_scopes")
    private Set<String> autoApproveScopes = Collections.emptySet();

    @Column(name = "authorities")
    private List<GrantedAuthority> authorities = Collections.emptyList();

    @Column(name = "access_token_validity_seconds")
    private Integer accessTokenValiditySeconds;

    @Column(name = "refresh_token_validity_seconds")
    private Integer refreshTokenValiditySeconds;

    @Column(name = "additional_information")
    private Map<String, Object> additionalInformation = new LinkedHashMap<String, Object>();

    @Override
    public String getClientId() {
        return this.clientId;
    }

    @Override
    public Set<String> getResourceIds() {
        return this.resourceIds;
    }

    @Override
    public boolean isSecretRequired() {
        return false;
    }

    @Override
    public String getClientSecret() {
        return this.clientSecret;
    }

    @Override
    public boolean isScoped() {
        return true;
    }

    @Override
    public Set<String> getScope() {
        return this.scope;
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        return this.authorizedGrantTypes;
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        return this.registeredRedirectUris;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return this.accessTokenValiditySeconds;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return this.refreshTokenValiditySeconds;
    }

    @Override
    public boolean isAutoApprove(String scope) {
        return true;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return this.additionalInformation;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    public void setScope(Set<String> scope) {
        this.scope = scope;
    }

    public void setResourceIds(Set<String> resourceIds) {
        this.resourceIds = resourceIds;
    }

    public void setAuthorizedGrantTypes(Set<String> authorizedGrantTypes) {
        this.authorizedGrantTypes = authorizedGrantTypes;
    }

    public void setRegisteredRedirectUris(Set<String> registeredRedirectUris) {
        this.registeredRedirectUris = registeredRedirectUris;
    }

    public void setAutoApproveScopes(Set<String> autoApproveScopes) {
        this.autoApproveScopes = autoApproveScopes;
    }

    public void setAuthorities(List<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public void setAccessTokenValiditySeconds(Integer accessTokenValiditySeconds) {
        this.accessTokenValiditySeconds = accessTokenValiditySeconds;
    }

    public void setRefreshTokenValiditySeconds(Integer refreshTokenValiditySeconds) {
        this.refreshTokenValiditySeconds = refreshTokenValiditySeconds;
    }

    public void setAdditionalInformation(Map<String, Object> additionalInformation) {
        this.additionalInformation = additionalInformation;
    }
}
